<?php
/**
 * @author Baranov Evgeny <baranov.evgeny@gmail.com>
 */

namespace Config;

use Exception;

abstract class Base {
    /**
     * @var \Config\Wrapper
     */
    private $data;

    /**
     * @var Base
     */
    private static $instance;

    /**
     * @param string $fileName
     * @param null $type
     * @throws \Exception
     */
    public static function load($fileName, $type = null) {
        if (!$type) {
            $type = pathinfo($fileName, PATHINFO_EXTENSION);
        }

        switch (strtolower($type)) {
            case 'ini':
                self::$instance = new INI($fileName);
                break;
            default:
                throw new Exception("Type '{$type}' of configuration file '{$fileName}' not supported");
        }
    }

    public static function instance() {
        return self::$instance;
    }

    /**
     * @param $data
     */
    protected function __construct(&$data) {
        $this->data = new Wrapper($data);
    }

    /**
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key) {
        return $this->data->$key;
    }

    /**
     * @param $key
     * @param $value
     */
    public function __set($key, $value) {
        $this->data->$key = $value;
    }

    /**
     * Convert config data to string
     *
     * @return string
     */
    public function __toString() {
        return $this->data->__toString();
    }
}
